package com.jingdianjichi.subject.handler;

import com.jingdianjichi.subject.common.enums.IsDeleteEnum;
import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import com.jingdianjichi.subject.convert.SubjectRadioConverter;
import com.jingdianjichi.subject.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.entity.SubjectInfoBO;
import com.jingdianjichi.subject.entity.SubjectOptionBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectBrief;
import com.jingdianjichi.subject.infra.basic.entity.SubjectRadio;
import com.jingdianjichi.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 单选题目的策略类
 */
@Component
public class RadioTypeHandler implements  SubjectTypeHandler{
    @Resource
    private SubjectRadioService subjectRadioService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //判断选项列表是否为空
        if(CollectionUtils.isEmpty(subjectInfoBO.getOptionList())) return;
        //单选题目的插入
        List<SubjectRadio> subjectRadios=new LinkedList<>();
        //遍历选项列表
        subjectInfoBO.getOptionList().forEach(option->{
            //将选项BO转换为选项实体
            SubjectRadio subjectRadio = SubjectRadioConverter.Instatnce.convertBOToRadio(option);
            //设置题目ID
            subjectRadio.setSubjectId(subjectInfoBO.getId());
            //设置是否删除
            subjectRadio.setIsDeleted(IsDeleteEnum.UN_DELETED.getCode());
            //将选项实体添加到列表中
            subjectRadios.add(subjectRadio);
        });
        //插入单选题目的表
        subjectRadioService.batchInsert(subjectRadios);
    }

    @Override
    public SubjectOptionBO query(Long id) {
        //获取简答题
        List<SubjectRadio> subjectRadios= subjectRadioService.queryBySubjectId(id);
        //将获取的信息 封装到OptionBo中
        SubjectOptionBO subjectOptionBO=new SubjectOptionBO();
        List<SubjectAnswerBO> subjectAnswerBOS = new ArrayList<>();
        subjectRadios.forEach(subjectRadio -> {
            SubjectAnswerBO subjectAnswerBO = new SubjectAnswerBO();
            subjectAnswerBO.setOptionType(subjectRadio.getOptionType());
            subjectAnswerBO.setOptionContent(subjectRadio.getOptionContent());
            subjectAnswerBO.setIsCorrect(subjectRadio.getIsCorrect());
            subjectAnswerBOS.add(subjectAnswerBO);
        });
        subjectOptionBO.setOptionList(subjectAnswerBOS);
        return subjectOptionBO;
    }
}
